package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.BankOrderManager;
import com.gopay.bis.manager.bank.ebank.KhPayManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.KhPayConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.khpay.ebank.KhPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 平安银行跨行支付业务实现类
 * 
 * @KhPayBankServiceImpl.java
 * @author zhengxue
 * @2017-8-14 上午10:22:39 www.gopay.com.cn Inc.All rights reserved.
 */
public class KhPayBankServiceImpl implements BankInteractRemoteService {
    @Resource(name = "khPayManager")
    KhPayManager khPayManager;

    @Resource(name = "bankOrderManager")
    private BankOrderManager bankOrderManager;

    @Override
    public Result pay(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.sign(message);
            result.setDefaultModel(retMsg);
            result.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
        }
        return result;
    }

    @Override
    public Result bankBack(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.verify(message);
            BankOrder bankOrder = bankOrderManager.getBankOrderById(retMsg.getBankOrderId());
            retMsg.setOrderId(bankOrder.getOrderId());
            result.setModel("res", retMsg);
            result.setDefaultModel(retMsg.isVerifyResult());
            result.setSuccess(true);
            Map<String, String> param = new HashMap<String, String>();

            if (retMsg.isVerifyResult() && KhPayConstants.STATUS_SUCCESS.equals(retMsg.getStatus())) {
                // 交易成功
                param.put("orderId", bankOrder.getOrderId());
                param.put("amt", retMsg.getAmount().toString());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "");
                param.put("signData", "");
                param.put("isFront", base.isFront() + "");
                result.setModel("isNotifyCashier", param);
                result.setOrderStatus(true);
            } else {
                param.put("orderId", bankOrder.getOrderId());
                param.put("amt", retMsg.getAmount().toString());
                param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "01");
                param.put("retCode", "01");
                param.put("retMsg", "");
                param.put("signData", "");
                param.put("isFront", base.isFront() + "");
                result.setModel("isNotifyCashier", param);
                result.setOrderStatus(false);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6031);
        }
        return result;
    }

    @Override
    public Result query(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.query(message);
            result.setDefaultModel(retMsg);
            result.setSuccess(true);
            if (BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
                Map<String, String> param = new HashMap<String, String>();
                if (retMsg == null) {
                    // 查询结果为空
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行返回对账结果为空");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                }
                param.put("orderId", retMsg.getOrderId());
                param.put("amount", retMsg.getAmount().toString());
                // 成功处理
                if (KhPayConstants.STATUS_SUCCESS.equals(retMsg.getStatus())) {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                    param.put("returnMessage", "交易成功");
                } else {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    param.put("returnMessage", "银行返回交易状态不明");
                }
                result.setModel("queryResult", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
    }

    @Override
    public Result refund(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.refund(message);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6087);
        }
        return result;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.query(message);
            result.setDefaultModel(retMsg);
            result.setSuccess(true);
            if (BisPayCheckUtils.checkExternalOrderID(base.getOrderId())) {
                Map<String, String> param = new HashMap<String, String>();
                if (retMsg == null) {
                    // 查询结果为空
                    param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
                    param.put("returnMessage", "银行返回对账结果为空");
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    result.setModel("queryResult", param);
                    return result;
                }
                param.put("orderId", retMsg.getOrderId());
                param.put("amount", retMsg.getRefundAmt());
                // 成功处理
                if (KhPayConstants.REFUND_STATUS_SUCCESS.equals(retMsg.getStatus())) {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
                    param.put("returnMessage", "交易成功");
                } else if (KhPayConstants.REFUND_STATUS_FAIL.equals(retMsg.getStatus())) {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
                    param.put("returnMessage", "交易失败");
                } else if (KhPayConstants.REFUND_STATUS_DEALING.equals(retMsg.getStatus())) {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    param.put("returnMessage", "银行返回交易处理中");
                } else {
                    param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
                    param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
                    param.put("returnMessage", retMsg.getErrorMsg());
                }
                result.setModel("queryResult", param);
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
    }

    @Override
    public Result dayend(BaseMessage base) {
        Result result = new Result(false);
        KhPayMessage message = new KhPayMessage();
        BeanUtils.copyProperties(base, message);
        try {
            KhPayMessage retMsg = (KhPayMessage) khPayManager.dayend(message);
            result.setDefaultModel(retMsg);
            result.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
    }

    @Override
    public Result sendBankRefund(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result queryBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

}
